<?php
/**
 * rss 生成类
 * @author yulipu
 */

class RSS {

    /**
     * RSS频道名
     */
    private $channel_title = '';

    /**
     * RSS频道链接
     */
    private $channel_atomLink = '';
    private $channel_link = '';

    /**
     * RSS频道描述
     */
    private $channel_description = '';

		private $lastBuildDate = '';
    /**
     * RSS频道所使用的语言
     */
    private $language = 'zh_CN';

    private $generator = ' Tailipu RSS Generator';

    /**
     * RSS单条信息的数组
     */
    private $items = array();

    /**
     * 构造函数
     * @param string $title  RSS频道名
     * @param string $atomLink RSS 订阅路径
     * @param string $link  RSS频道链接
     * @param string $description  RSS频道描述
     */
    public function __construct($title, $atomLink, $link, $description) {
        $this->channel_title = $title;
        $this->channel_atomLink = $atomLink;
        $this->channel_link = $link;
        $this->channel_description = $description;
        
        $this->lastBuildDate = date('Y-m-d H:i:s', time());
    }

    /**
     * 添加RSS项
     * @param string $title  标题
     * @param string $link  链接
     * @param string $author  作者
     * @param string $pubDate  文章日期
     * @param string $description  摘要
     * @param string $content  内容
     */
     function addItem($title, $link, $author, $pubDate, $description, $content) {
        $this->items[] = array('title' => $title, 'link' => $link, 'author' => $author, 'pubDate' => $pubDate, 'description' => $description, 'content'=> $content);
     }

 

    /**
     * 输出RSS的XML为字符串
     */
    public function fetch() {
        $rss = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n";
        $rss = "<rss xmlns:content=\"http://purl.org/rss/1.0/modules/content/\" xmlns:wfw=\"http://wellformedweb.org/CommentAPI/\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:atom=\"http://www.w3.org/2005/Atom\" xmlns:sy=\"http://purl.org/rss/1.0/modules/syndication/\" xmlns:slash=\"http://purl.org/rss/1.0/modules/slash/\" version=\"2.0\">\r\n";
        $rss .= "<channel>\r\n";
        $rss .= "<title>{$this->channel_title}</title>\r\n";
        $rss .= "<atom:link href=\"{$this->channel_atomLink}\" rel=\"self\" type=\"application/rss+xml\"/>\r\n";
        $rss .= "<link>{$this->channel_link}</link>\r\n";
		$rss .= "<pubDate>{$this->lastBuildDate}</pubDate>\r\n";
		$rss .= "<lastBuildDate>{$this->lastBuildDate}</lastBuildDate>\r\n";
        $rss .= "<description>{$this->channel_description}</description>\r\n";
        $rss .= "<language>{$this->language}</language>\r\n";  
		$rss .= "<sy:updatePeriod>daily</sy:updatePeriod>\r\n";
		$rss .= "<sy:updateFrequency>1</sy:updateFrequency>\r\n";
		$rss .= "<copyright>{$this->generator}</copyright>\r\n";
		$rss .= "<generator>{$this->generator}</generator>\r\n";

        for ($i = 0; $i < count($this->items); $i++) {
            $rss .= "<item>\r\n";
            $rss .= "<title>{$this->items[$i]['title']}</title>\r\n";
            $rss .= "<link>{$this->items[$i]['link']}</link>\r\n";
						$rss .= "<dc:creator>{$this->items[$i]['author']}</dc:creator>\r\n";
						$rss .= "<pubDate>{$this->items[$i]['pubDate']}</pubDate>\r\n";
            $rss .= "<guid isPermaLink=\"false\">{$this->items[$i]['link']}</guid>\r\n";
            $rss .= "<description><![CDATA[{$this->items[$i]['description']}]]></description>\r\n"; 
            $rss .= "<content:encoded><![CDATA[{$this->items[$i]['content']}]]></content:encoded>\r\n";
            $rss .= "</item>\r\n";

        }

        $rss .= "</channel>\r\n</rss>";

        return $rss;

    }

 

    /**
     * 输出RSS的XML到浏览器
     */
    public function show() {
        header("Content-Type: text/xml; charset=utf-8");
        echo $this->fetch();
        exit;
    }

}

?>
